//
//  MainTabBar.swift
//  Weibo
//
//  Created by mac on 15/11/26.
//  Copyright © 2015年 zsl. All rights reserved.
//

import UIKit

//支持手码和Xib及Sb创建
class MainTabBar: UITabBar {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
       // fatalError("init(coder:) has not been implemented")
        
        //如果希望自定义视图 既支持手码创建 也支持 XIB/SB创建 可以将默认报错 注释
        super.init(coder: aDecoder)
        setUpUI()
    }
    
    private func setUpUI(){
        
        addSubview(composeBtn)
        
    }
    
    /**
    *  懒加载 撰写按钮
    */
    lazy var composeBtn: UIButton = {
        
        let btn = UIButton()
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
       
        btn.sizeToFit()
        return btn
    
    }()
    //布局子控件
    override func layoutSubviews() {
        super.layoutSubviews()
        let w = screenW / 5
        let h = self.bounds.height
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        
        var index:CGFloat = 0
        for v in subviews{
            if v.isKindOfClass(NSClassFromString("UITabBarButton")!){
           
            v.frame = CGRectOffset(rect, w * index, 0)
                if index == 1 {
                    index++
                }
                index++
                //三目运算的简单写法
                //index += (index == 1 ? 2 : 1)
            
            }
            //print(v)
        }
        //设置撰写btn的frame
        
        //composeBtn.frame = CGRectOffset(rect, w * index, -20)
        //bringSubviewToFront(composeBtn)
        
        composeBtn.frame = CGRectOffset(rect, 2 * w, 0)
        
    }

}
